package com.example.controller;

import com.example.result.ReturnCode;
import com.example.result.ReturnResult;
import com.example.service.IAttributeManagementService;
import com.example.vo.AttributeCreateFormVO;
import com.example.vo.AttributeQueryViewVO;
import com.example.vo.AttributeUpdateFormVO;
import com.huawei.innovation.rdm.xdm.dto.entity.EXADefinitionQueryViewDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/attribute")
public class AttributeManagementController {
    @Autowired
    private IAttributeManagementService attributeManagementService;
    @GetMapping("/query")
    public ReturnResult queryAttribute(@RequestParam(required = false) String name){
        List<AttributeQueryViewVO> r = attributeManagementService.queryAttributeByName(name);
        return new ReturnResult(ReturnCode.GET_OK,"查询属性成功",r);
    }
    @PostMapping("/create")
    public ReturnResult create(@RequestBody AttributeCreateFormVO createForm){
        return attributeManagementService.create(createForm);
    }
    @PutMapping("/update")
    public ReturnResult update(@RequestBody AttributeUpdateFormVO form){
        return attributeManagementService.update(form);
    }
    @DeleteMapping("/delete")
    public ReturnResult delete(@RequestBody List<String> ids){
        List<Long> ids_ = new ArrayList<>();

        // 遍历原始列表并转换每个元素
        for (String id : ids) {
            try {
                long longId = Long.parseLong(id); // 将字符串转换为Long
                ids_.add(longId);
            } catch (NumberFormatException e) {
                // 如果转换失败，可以打印错误信息或者进行其他错误处理
                System.err.println("无法将 " + id + " 转换为Long类型");
            }
        }
        return attributeManagementService.batchDeleteByIds(ids_);
    }

}
